<?php
	namespace Qzn\View;
	class LoginView {
		private $m_getPassword = "password";
		private $m_getUsername = "username";
		private $m_getRememberMe = "checkbox";
		private $m_loginButton = "loginbutton";
		private $m_logoutButton = "logoutbutton";
		private $m_forgettButton = "forgettbutton";
		private $m_getForgettUsername = "username";
		public $m_usernameCookie = "user";
		public $m_passwordCookie = "pw";
		
		
		/**
		 * @return string HTML loginBox
		 */
		public function DoLoginBox() {
			return "<div>
						<form method='post'>
							<fieldset>
								<label for='$this->m_getUsername'>Username: </label>
								<input name='$this->m_getUsername' type='text'/><br />
								<label for='$this->m_getPassword'>Password: </label>
								<input name='$this->m_getPassword' type='password'/><br />
								<input name='$this->m_loginButton' type='submit' value='Login'> 
								<label for='$this->m_getRememberMe'>Remember me </label>
								<input name='$this->m_getRememberMe' type='Checkbox' />
								<a href='?register'>Register here!</a>
								<a href='?forgotpw'>Forgot Password?</a>
							</fieldset>
						</form>
					</div>";
		}
		
		/**
		 * @return string HTML loginBox
		 */
		public function DoResetSuccessBox($pw) {
			return "<p>Password reset! Your new password is: $pw</p>";
		}
		
		/**
		 * @return string HTML loginBox
		 */
		public function DoForgettBox() {
			return "<div>
						<form method='post'>
							<fieldset>
								<label name='$this->m_getForgettUsername'>Username: </label>
								<input name='$this->m_getForgettUsername' type='text'> 
								<input name='$this->m_forgettButton' type='submit' value='Reset Password'> 
							</fieldset>
						</form>
					</div>";
		}
		
		/**
		 * @return string HTML logoutBox
		 */
		public function DoLogoutBox() {
			$wUser = $this->GetUserName();
			$wPk = $this->GetUserPk();
			return "<div>
						<form method='post'>
							<fieldset>
								Welcome <a href='?profile=$wPk'>$wUser<a>
								<a href='?'>Home</a>
								<a href='?member'>Member List</a>
								<a href='?friendlist=$wPk'>Friend List</a>
								<input type='submit' name='$this->m_logoutButton' value='Logout'/>
							</fieldset>
						</form>
					</div>";
		}
		
		/**
		 * Gets username from input data
		 * @return string Username from input
		 */
		public function GetForgettUsername() {
			if(isset($_POST[$this->m_getForgettUsername])) {
				return $_POST[$this->m_getForgettUsername];
			}
			return NULL;
		}
		
		/**
		 * Gets username from input data
		 * @return string Username from input
		 */
		public function GetUserName() {
			if(isset($_POST[$this->m_getUsername])) {
				return $_POST[$this->m_getUsername];
			} else if (isset($_SESSION[\Qzn\Model\LoginHandler::$m_login])) {
				return $_SESSION[\Qzn\Model\LoginHandler::$m_login];
			}
			return NULL;
		}
		
		/**
		 * Gets password from input data
		 * @return string Password from input
		 */
		public function GetPassword() {
			if(isset($_POST[$this->m_getPassword])) {
				return $_POST[$this->m_getPassword];
			}
			return NULL;
		}
		
		/**
		 * Gets userPk from input data
		 * @return string Password from input
		 */
		public function GetUserPk() {
			if(isset($_SESSION[\Qzn\Model\LoginHandler::$m_userPk])) {
				return $_SESSION[\Qzn\Model\LoginHandler::$m_userPk];
			}
			return NULL;
		}
		
		/**
		 * Login success
		 * @return string html
		 */
		public function SuccessfullLogin($user, $tempPw) {
			if ($this->GetRememberMe()) {
				$this->SetCookie($this->m_usernameCookie, $user);
				$this->SetCookie($this->m_passwordCookie, $tempPw);
			}
		}
		
		/**
		 * Failed to login
		 * @return string html
		 */
		public function failedLogin() {
		}
		
		/**
		 * Logsout the user
		 */
		public function SuccessfullLogout() {
		 	unset($_SESSION[\Qzn\Model\LoginHandler::$m_login]);
			$this->KillCookie($this->m_usernameCookie);
			$this->KillCookie($this->m_passwordCookie);
		}
		
		/**
		 * @return int $userCat
		 */
		public function GetUserCat(){
			if(isset($_SESSION[\Qzn\Model\LoginHandler::$m_userCat])) {
				return $_SESSION[\Qzn\Model\LoginHandler::$m_userCat];
			}
			return false;
		}
		
		/**
		 * @return boolean
		 */
		public function GetRememberMe(){
			if(isset($_POST[$this->m_getRememberMe])) {
				return true;
			}
			return false;
		}
		
		/**
		 * @param string $cookie
		 * @param $value
		 */
		public function SetCookie($cookie, $value) {
			setcookie($cookie, $value, time()+2592000);
		}
		
		/**
		 * @param string $cookie
		 */
		public function KillCookie($cookie) {
			setcookie($cookie, "", time()-2592000);
		}
		
		/**
		 * @param string $cookie
		 * @return $value
		 */
		public function GetCookie($cookie) {
			if(isset($_COOKIE[$cookie])) {
				return $_COOKIE[$cookie];
			}
			return null;
		}
		
		/**
		 * @param int $errorCode
		 * @return string $html
		 */
		public function ErrorHandler($errorCode) {
			switch ($errorCode) {
				case \Qzn\Model\fileUploadHandler::EVERYTHING_WENT_BETTER_THEN_EXPECTED:
						$html = "File upload success!";
					break;
				case \Qzn\Model\fileUploadHandler::GENERIC_ERROR:
						$html = "Error: Upload failed.";
					break;
				case \Qzn\Model\fileUploadHandler::EXTENSION_NOT_SUPPORTED:
						$html = "Error: That exstension is not supported.";
					break;
				case \Qzn\Model\fileUploadHandler::FAILED_TO_MOVE:
						$html = "Error: Failed to move file.";
					break;
				case \Qzn\Model\fileUploadHandler::FILE_TO_BIG:
						$html = "Error: File is to big, it needs to be less then 1mb.";
					break;
				case \Qzn\Model\loginHandler::EVERYTHING_WENT_BETTER_THEN_EXPECTED:
						$html = "Login successful.";
					break;
				case \Qzn\Model\loginHandler::USERNAME_DOSNT_EXIST:
						$html = "Username dosnt exist.";
					break;
				case \Qzn\Model\loginHandler::WRONG_PASSWORD:
						$html = "Wrong password.";
					break;
				
			}
			return $html;
		}
		
		/**
		 * Gets loginButton from input data
		 * @return boolean loginButton from input data
		 */
		public function TriedToLogin() {
			if(isset($_POST[$this->m_loginButton])) {
				return true;
			}
			return false;
		}
		
		/**
		 * Gets loginButton from input data
		 * @return boolean loginButton from input data
		 */
		public function TriedToReset() {
			if(isset($_POST[$this->m_forgettButton])) {
				return true;
			}
			return false;
		}
		
		/**
		 * Gets logoutButton from input data
		 * @return boolean logoutButton from input data
		 */
		public function TriedToLogout() {
			if(isset($_POST[$this->m_logoutButton])) {
				return true;
			}
			return false;
		}
		
		/**
		 * Echo test data
		 * @return boolean
		 */
		public static function Test() {
			$lw = new LoginView();
						
			$lw->KillCookie("test");
			
			$lw->SetCookie("test", 12);
			
			if ($lw->GetCookie("test") != 12) {
				echo "Något gick galet.";
				return false;
			}
			
			return true;
		}
	}
?>